package io.xxx.xbutler.config;

import io.xxx.xbutler.sdk.RedissonTokenProvider;
import io.xxx.xbutler.sdk.TanJingClient;
import io.xxx.xbutler.sdk.TokenProvider;
import jakarta.annotation.Resource;
import org.redisson.api.RedissonClient;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
@EnableConfigurationProperties(TanJingProperties.class)
public class TanJingClientConfig {

    @Resource
    private TanJingProperties properties;

    @Resource
    private RedissonClient redissonClient;

    @Bean
    public TanJingClient tanJingClient(RestTemplateBuilder builder) {
        RestTemplate restTemplate = builder.build();
        TanJingProperties.Merchant merchant = properties.getMerchant();
        TokenProvider tokenProvider = new RedissonTokenProvider(redissonClient, restTemplate);
        return new TanJingClient(merchant.getId(), merchant.getSecret(), tokenProvider, restTemplate);
    }
}
